package classificator;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;


public class MainTest2 {
    /**
     * @param args
     * @throws IOException 
     * @throws ClassNotFoundException 
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //TODO ip & porta devono essere acquisiti come parametri(txt da leggere?)   
        int port = 8080;
        String ip = "127.0.0.1";
        int scelta;
        InetAddress addr = InetAddress.getByName(ip);
        Socket socket = new Socket(addr, port);
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        // stream con richieste del client
        ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); 
        do {
            System.out.println("1. Apprende DT mysql");
            System.out.println("2. Salva DT in file.dat");
            System.out.println("3. Ripristina DT da file.dat");
            System.out.println("4. Use Decision Tree for Prediction :");
            System.out.println("5. Exit :");
            System.out.print("Scelta: :");
            scelta = Keyboard.readInt();
            Object outputVal = null;
            switch(scelta){
                case 1:
                    System.out.print("scrivere nome tabella:");
                    String name = Keyboard.readString();
                    out.writeObject("1" + name);
                    outputVal = in.readObject();
                    while(!(outputVal instanceof Exception || ((String)outputVal).equals("Learning DT..."))) {
                        System.out.println(outputVal);				
                        out.writeObject(new Integer(Keyboard.readInt()));
                        outputVal=in.readObject();
                    }
                    if (outputVal instanceof String)
                        outputVal=in.readObject();
                    break;
                case 2:
                    out.writeObject(new Integer(2));
                    outputVal = in.readObject();
                    while(!(outputVal instanceof Exception || ((String)outputVal).equals("Saving current DT into file..."))) {
                        System.out.println(outputVal);				
                        out.writeObject(new Integer(Keyboard.readInt()));
                        outputVal=in.readObject();
                    }
                    if (outputVal instanceof String)
                        outputVal=in.readObject();
                    break;
                case 3:
                    out.writeObject(new Integer(3));
                    outputVal = in.readObject();
                    while(!(outputVal instanceof Exception || ((String)outputVal).equals("Store DT from file..."))) {
                        System.out.println(outputVal);				
                        out.writeObject(new Integer(Keyboard.readInt()));
                        outputVal=in.readObject();
                    }
                    if (outputVal instanceof String)
                        outputVal=in.readObject();
                    break;
                case 4:
                    out.writeObject(new Integer(4));
                    outputVal = in.readObject();
                    while(!(outputVal instanceof Exception || ((String)outputVal).equals("Transmitting class ..."))) {
                        System.out.println(outputVal);				
                        out.writeObject(new Integer(Keyboard.readInt()));
                        outputVal=in.readObject();
                    }
                    if (outputVal instanceof String)
                        outputVal=in.readObject();
                    break;
                case 5:
                    System.exit(0);
                    break;
                default:
                    outputVal = new String("Scelta non valida");
            } //end switch
            System.out.println(outputVal);
        } while (scelta!=5);
    }	
}